package industry_2024.industry_04.extract

import org.apache.spark.sql.SparkSession

object table_preparation {
  def main(args: Array[String]): Unit = {
    val spark=SparkSession.builder()
      .master("local[*]")
      .appName("数据抽取的表格准备")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .enableHiveSupport()
      .getOrCreate()

    //  数据库必须要在虚拟机里面创建好，在这里创建不行
    spark.sql("use hudi_gy_ods04")

    //  changerecord
    spark.sql("drop table if exists changerecord")
    spark.sql(
      """
        |create table if not exists changerecord(
        |ChangeID int,
        |ChangeMachineID int,
        |ChangeMachineRecordID int,
        |ChangeRecordState string,
        |ChangeStartTime timestamp,
        |ChangeEndTime timestamp,
        |ChangeRecordData string,
        |ChangeHandleState int
        |)using hudi
        |tblproperties(
        |type="cow",
        |primaryKey="ChangeID,ChangeMachineID",
        |preCombineField="ChangeEndTime",
        |hoodie.datasource.hive_aync.mode="hms"
        |)
        |partitioned by(etldate string)
        |""".stripMargin)

    //  basemachine
    spark.sql("drop table if exists basemachine")
    spark.sql(
      """
        |create table if not exists basemachine(
        |BaseMachineID int,
        |MachineFactory int,
        |MachineNo string,
        |MachineName string,
        |MachineIP string,
        |MachinePort int,
        |MachineAddDate timestamp,
        |MachineRemarks string,
        |MachineAddEmpID int,
        |MachineResponsEmpID int,
        |MachineLedgerXml string,
        |ISWS int
        |)using hudi
        |tblproperties(
        |type="cow",
        |primaryKey="BaseMachineID",
        |preCombineField="MachineAddDate",
        |hoodie.datasource.hive_aync.mode="hms"
        |)
        |partitioned by(etldate string)
        |""".stripMargin)
    //  machinedata
    spark.sql("drop table if exists machinedata")
    spark.sql(
      """
        |create table if not exists machinedata(
        |MachineRecordID int,
        |MachineID int,
        |MachineRecordState string,
        |MachineRecordData string,
        |MachineRecordDate timestamp
        |)using hudi
        |tblproperties(
        |type="cow",
        |primaryKey="MachineRecordID",
        |preCombineField="MachineRecordDate",
        |hoodie.datasource.hive_aync.mode="hms"
        |)
        |partitioned by(etldate string)
        |""".stripMargin)

    //  producerecord
    spark.sql("drop table if exists producerecord")
    spark.sql(
      """
        |create table if not exists producerecord(
        |ProduceRecordID int,
        |ProduceMachineID int,
        |ProduceCodeNumber string,
        |ProduceStartWaitTime timestamp,
        |ProduceCodeStartTime timestamp,
        |ProduceCodeEndTime timestamp,
        |ProduceCodeCycleTime int,
        |ProduceEndTime timestamp,
        |ProduceTotalOut int,
        |ProduceInspect int
        |)using hudi
        |tblproperties(
        |type="cow",
        |primaryKey="ProduceRecordID,ProduceMachineID",
        |preCombineField="ProduceCodeEndTime",
        |hoodie.datasource.hive_aync.mode="hms"
        |)
        |partitioned by(etldate string)
        |""".stripMargin)

    spark.sql("show tables").show



    spark.close()
  }

}
